package interceptors

import (
	"aquaman/app/infra/job"
	"aquaman/app/infra/log"
	"context"
	"fmt"
)

func RecoveryInterceptor(logger log.Logger) job.TaskInterceptor {
	return func(next job.Task) job.Task {
		return func(ctx context.Context) (err error) {
			defer func() {
				if r := recover(); r != nil {
					logger.Warnf("%T %s", r, r)
					if e, ok := r.(error); ok {
						err = e
					} else {
						err = fmt.Errorf("%T %s", r, r)
					}
				}
			}()

			return next(ctx)
		}
	}
}
